package com.mall.db.api;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.SmsHomeRecommendProductMapper;
import com.mall.domain.SmsHomeRecommendProduct;
import com.mall.interface1.api.SmsHomeRecommendProductServiceApi;
import com.mall.vo.SmsHomePageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SmsHomeRecommendProductServiceApiImpl implements SmsHomeRecommendProductServiceApi {

    @Autowired
    SmsHomeRecommendProductMapper smsHomeRecommendProductMapper;


    @Override
    public SmsHomePageBeanVo findByPage(String productName, Integer pageNum, Integer pageSize, Integer recommendStatus) {
        Page<SmsHomeRecommendProduct> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeRecommendProduct> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(productName),SmsHomeRecommendProduct::getProductName,productName);
        qw.eq(recommendStatus != null, SmsHomeRecommendProduct::getRecommendStatus, recommendStatus);
        qw.orderByDesc(SmsHomeRecommendProduct::getSort);

        Page<SmsHomeRecommendProduct> smsHomeRecommendProductPage = smsHomeRecommendProductMapper.selectPage(page, qw);
        return new SmsHomePageBeanVo(pageNum,pageSize,smsHomeRecommendProductPage.getTotal(),smsHomeRecommendProductPage.getRecords());
    }

    @Override
    public void updateRecommendStatus(Long id, Integer recommendStatus) {
        SmsHomeRecommendProduct smsHomeRecommendProduct = smsHomeRecommendProductMapper.selectById(id);
        smsHomeRecommendProduct.setRecommendStatus(recommendStatus);
        smsHomeRecommendProductMapper.updateById(smsHomeRecommendProduct);
    }

    @Override
    public void addCreate(SmsHomeRecommendProduct smsHomeRecommendProduct) {
        smsHomeRecommendProductMapper.insert(smsHomeRecommendProduct);
    }

    @Override
    public void delete(Long id) {
        smsHomeRecommendProductMapper.deleteById(id);
    }

    @Override
    public void updateSort(Long id, Integer sort) {
        SmsHomeRecommendProduct smsHomeRecommendProduct = smsHomeRecommendProductMapper.selectById(id);
        smsHomeRecommendProduct.setSort(sort);
        smsHomeRecommendProductMapper.updateById(smsHomeRecommendProduct);
    }


}
